package com.baizhi.springboot.controller;

import com.baizhi.springboot.entity.User;
import com.baizhi.springboot.service.UserService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getUserProvince")
    @ResponseBody
    public List<Map> getUserProvince() {
        return userService.getUserProvince();
    }
    @RequestMapping("/getUserSex")
    @ResponseBody
    public List<Map> getUserSex() {
        return userService.getUserSex();
    }
    @RequestMapping("/selectUserByPage")
    @ResponseBody
    public Map selectUserByPage(Integer page, Integer limit) {
        return userService.selectUserByPage(page,limit);
    }
    @RequestMapping("/daoru")
    @ResponseBody
    public Map daoru(MultipartFile file) throws Exception {
        System.out.println(file.getOriginalFilename()+"============");
        return userService.daoru(file.getInputStream());

    }
    @RequestMapping("/daochu")
    public void daochu(HttpServletResponse response) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, IOException {
        HSSFWorkbook workbook = userService.daochu();
        response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode("user.xls","utf-8"));
        workbook.write(response.getOutputStream());
    }
    @RequestMapping("/updateUserStatus")
    @ResponseBody
    public Map updateUserStatus(Integer id) {
        HashMap<Object, Object> map = new HashMap<>();

        try {
            userService.updateUserStatus(id);
            map.put("isUpdate", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate", false);
        }
        return map;
    }

}
